package com.rural.bus;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.rural.bus.model.Railway;
import com.rural.bus.model.SpecRailway;
import com.rural.bus.net.MyHttpUtil;
import com.rural.bus.net.OnNetCallback;
import com.rural.bus.net.Url;
import com.rural.bus.utils.DateTimePickDialogUtil;
import com.rural.bus.utils.DateUtil;
import com.rural.bus.utils.ProgressUtil;
import com.rural.bus.view.SlideItemDialog;
import com.rural.bus.view.SlideItemDialog.OnDialogDismissListener;

public class SpecRailwayActivity extends BaseRailwayActivity {
	
	private TextView mTvRoute;
	private SlideItemDialog mRouteDialog;
	private String [] arr;
	private List<SpecRailway> railways=new ArrayList<SpecRailway>();
	
	/***
	 * 路线id
	 */
	private int mRouteId;
	
	private SpecRailway mRailway;
	
	@Override
	protected void initView() {
		super.initView();
		
		mTvRoute=(TextView) findViewById(R.id.tv_route);
		mTvRoute.setVisibility(View.VISIBLE);
	}
	
	@Override
	protected void setListeners() {
		super.setListeners();
		
		mBtSubmit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				taketime = DateUtil.getDate(mTvTime.getText().toString(), "yyyy年MM月dd日 HH:mm");

//				if (taketime == 0) {
//					toast("基本信息为空");
//					return;
//				}
				if (isSelf) {
					passengerName = mCurrentUser.name;
					passengerPhone = mCurrentUser.phone;
				} else {
					passengerName = etPassengerName.getText().toString();
					passengerPhone = etPassengerPhone.getText().toString();
				}
				if (TextUtils.isEmpty(passengerName) || TextUtils.isEmpty(passengerName)) {
					toast("基本信息为空");
					return;
				}
				String remarks = mEtRemarks.getText().toString();
				ProgressUtil.showProgress(SpecRailwayActivity.this, "");
				mCurrentRailway = new Railway();
				mCurrentRailway.call(new Integer(mCurrentUser.userId), mCurrentUser.name,mRailway.depart,0, 0,mRailway.dest, 0,
						0, taketime, number, 0, 4, 0, mRouteId, passengerPhone, remarks, new OnNetCallback() {

							@Override
							public void success(JSONObject object) {
								ProgressUtil.closeProgress();

								mCurrentUser.currentRailway = mCurrentRailway;
								// 叫车成功
								try {
									// 正常状态
									if (object.getInt("callStatus") == 0) {
										startActivity(WaitActivity.class);
									} else if (object.getInt("callStatus") == 2) {
										toast("附近没有司机");
									} else {
										toast("发生未知错误");
									}
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}

							@Override
							public void fail(String error) {
								toast(error);
								ProgressUtil.closeProgress();

							}
						});
			}
		});
		
		mTvTime.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				timeDialog();
			}
		});
		
		mTvRoute.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if (mRouteDialog == null) {
					mRouteDialog = new SlideItemDialog(SpecRailwayActivity.this, "选择线路",arr, 0, new OnDialogDismissListener() {

						@Override
						public void onConfirmClicked(int index) {
							mTvRoute.setText("线路选择：  "+arr[index]);
							mRouteId=railways.get(index).id;
							mRailway=railways.get(index);
							check();
						}

						@Override
						public void onCancelClicked() {

						}

					});
				}
				mRouteDialog.show();
			}
		});
		
		mTvTime.addTextChangedListener(mWatcher);
		mTvNumber.addTextChangedListener(mWatcher);
//		mTvRoute.addTextChangedListener(mWatcher);

	}	
	
	private TextWatcher mWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

		}

		@Override
		public void afterTextChanged(Editable arg0) {
			check();
		}
	};


	protected void timeDialog() {
		Calendar c=Calendar.getInstance();
		String initEndDateTime=c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"
				+c.get(Calendar.DAY_OF_MONTH)+"日 "+c.get(Calendar.HOUR_OF_DAY)+":"+c.get(Calendar.MINUTE);
		 DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(  
                 this, initEndDateTime);  
         dateTimePicKDialog.dateTimePicKDialog(mTvTime);  
	}
	
	@Override
	protected void initData() {
		super.initData();
		ProgressUtil.showProgress(this, "");
		MyHttpUtil http=new MyHttpUtil();
		http.send(Url.show_url, new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				ProgressUtil.closeProgress();
				Log.d(TAG, object.toString());
				int i=0;
				try {
						JSONArray array=object.getJSONArray("result");
						while(i<array.length()){
							JSONObject jObject=array.getJSONObject(i);
							SpecRailway railway=new SpecRailway();
							railway.id=jObject.getInt("id");
							railway.depart=jObject.getString("name");
							railway.dest=jObject.getString("name2");
	                         railways.add(railway);
							i++;
						}
						
				} catch (JSONException e) {
					e.printStackTrace();
				}finally{
					if(railways.size()==0){
						toast("暂未开通专线");
						finish();
					}
					arr=new String [railways.size()];
					i=0;
					for(SpecRailway railway:railways){
						arr[i]=railway.depart+" - "+railway.dest;
						i++;
					}
				}
			}
			
			@Override
			public void fail(String error) {
				ProgressUtil.closeProgress();
				toast("网络异常");
				finish();
			}
		});
	}
	
	/***
	 * 预估价格
	 */
	private void check(){
		if(mRouteId==0){
			return;
		}
		ProgressUtil.showProgress(this, "");
		Railway.check(new Integer(mCurrentUser.userId), taketime, number, carTypeIndex, 4, 0, mRouteId, new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				ProgressUtil.closeProgress();
                try {
					int result=object.getInt("result");
					mTvPrice.setText("约"+result+"元");
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void fail(String error) {
				ProgressUtil.closeProgress();
				try {
					JSONObject  object = new JSONObject(error);
					toast(object.getString("msg"));
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
		});
	}

}
